Imports Cdm.DataCore.Entities

Partial Class Case_caseLegalGuardian
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return "Legal Guardian"
        End Get
    End Property

    Private ReadOnly Property CurrentPermission() As Cdm.DataCore.UserSecurity.Permission.PermissionType
        Get
            If TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.LegalGuardian Then
                Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseGuardianPermissions
            ElseIf TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.ParticipantRepresentative Then
                Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseRepPermissions
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property IsLegalGuardianChecked() As Boolean
        Get
            Return Me.HasLegalGuardianCheckBox.Checked
        End Get
    End Property

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseLegalGuardian
        End Get
    End Property

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Dim pg As IPermission = Me.Page
            Dim pt As Participant = pg.PrimaryObject
            Return pt.Guardian
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            BindDropDowns()
            SetPageControls()
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        If Me.IsPostBack Then
            EntityController.SetPagePropertiesFromObject(Me, False)
            Me.SpecialFieldControl.Editable = CheckEditable()
            Me.SpecialFieldControl.PrimaryObject = Me.PrimaryObject
        Else
            Dim legalGuardianTitle As String = EntityController.GetSingleDisplayName("Legal Guardian")
            Me.TitleLabel.Text = legalGuardianTitle
            Me.HasLegalGuardianLabel.Text = "Has " & legalGuardianTitle & "?"
        End If
    End Sub

    ''' <summary>
    ''' Checks if the 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckEditable() As Boolean
        If Me.PrimaryObject.Id > 0 Then
            Return UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        Else
            Return UserAuthorization.HasInsertPermission(Me.EntityPermissionType)
        End If

    End Function

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        If UserAuthorization.HasViewPermission(Me.CurrentPermission) Then
            Me.PermissionsButton.Visible = True
        Else
            Me.PermissionsButton.Visible = False
        End If

    End Sub

    Public Sub ResetControls() Implements IPermission.ResetControls

    End Sub

    Private Sub BindDropdowns()

        Me.LegalguardianTypeInputControl.DataSource = Cdm.DataCore.LookupTables.LuLegalGuardianType.ListAll()
        Me.LegalguardianTypeInputControl.DataBind()

    End Sub

    Private Sub SetPageControls()

        EntityController.SetPagePropertiesFromObject(Me, True)
        Me.SpecialFieldControl.Editable = CheckEditable()
        Me.SpecialFieldControl.PrimaryObject = Me.PrimaryObject
        Dim pg As IPermission = Me.Page
        Dim pt As Participant = pg.PrimaryObject
        If pt.ParticipantId > 0 Then
            Me.HasLegalGuardianCheckBox.Enabled = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        Else
            Me.HasLegalGuardianCheckBox.Enabled = UserAuthorization.HasInsertPermission(Me.EntityPermissionType)
        End If
        Me.GuardianPanel.Enabled = pt.HasLegalGuardian
        Me.HasLegalGuardianCheckBox.Checked = pt.HasLegalGuardian

    End Sub

    Public Sub SaveData()

        EntityController.SetObjectPropertiesFromPage(Me)
        Me.PrimaryObject.SpecialFieldsValues = Me.SpecialFieldControl.SaveSpecialFields()
        Me.RepGuardianPermissions.SetPermissions()

    End Sub

    Protected Sub hasLegalGuardian_CheckedChanged(ByVal sender As Object, _
                                                  ByVal e As System.EventArgs) _
                                                  Handles HasLegalGuardianCheckBox.CheckedChanged

        Me.GuardianPanel.Enabled = Me.HasLegalGuardianCheckBox.Checked

    End Sub

    Protected Sub PermissionsButton_Click(ByVal sender As Object, _
                                          ByVal e As System.EventArgs) _
                                          Handles PermissionsButton.Click

        If Me.PermissionsButton.Text = "Show Permissions" Then

            With Me.RepGuardianPermissions
                .Visible = True
                .BindGridView()
                .SetPermissions()
                .CheckRolePermissions()
            End With
            Me.PermissionsButton.Text = "Hide Permissions"
            Me.WholePagePanel.Visible = False

        Else

            Me.RepGuardianPermissions.Visible = False
            Me.PermissionsButton.Text = "Show Permissions"
            Me.WholePagePanel.Visible = True

        End If

    End Sub

End Class
